// 截取的id

console.log($().jquery); // => '3.6.0'
var $jq = jQuery.noConflict(true);
// 现在window.$和window.jQuery被恢复成1.5版本:
console.log($().jquery); // => '1.5.0'
console.log($jq().jquery);

const id = location.href.split("=")[1];
console.log(id);
const Detail = localStorage.getItem("Detail" + id);
console.log(Detail);
const Msg = JSON.parse(Detail);
console.log(Msg.src);
// console.log(src);
$("#smil").attr("href", Msg.src);
$("#smil .img").attr("src", Msg.src);
$(".main-right .p1 p").html(Msg.title);
$(".main-right .p2 p").html(Msg.nameads);
$(".main-right .p3").html(Msg.price);
$(".main-right  del").html(Msg.price);

var token = cookie("Token");
let datalist = "";
let goods_id = "";
axios.get("../static/data/goodsList.json").then((res) => {
  datalist = res.data;
  let item = "";
  for (var i = 0; i < res.data.length; i++) {
    if (parseInt(datalist[i].id) === parseInt(id)) {
      console.log(datalist[i]);
      item = datalist[i];
      goods_id = item.id;
    }
  }

  $(".goumai").on("click", () => {
    console.log(1);

    let Token = cookie("Token");
    console.log("token ===", Token === "");
    if (!(Token === "")) {
      console.log("一登陆！");
      addCart(goods_id, item);
      alert("成功加入购物车");
      location.href = "./gwc2.html";
    } else {
      console.log("未登录");
      location.href = "login.html";
    }
  });
  $(".gouwuche").on("click", () => {
    console.log(1);

    let Token = cookie("Token");
    console.log("token ===", Token === "");
    if (!(Token === "")) {
      console.log("一登陆！");
      addCart(goods_id, item);
      alert("成功加入购物车");
    } else {
      console.log("未登录");
      location.href = "login.html";
    }
  });

  let cart_data = getId();

  function getId() {
    try {
      let data = JSON.parse(localStorage.getItem("cart"));
      if (data === null) {
        return {};
      }
      return data;
    } catch (e) {
      return {};
    }
  }

  function addCart(id, item) {
    //   console.log(item);
    //   console.log(cart_data[id]);
    if (id in cart_data) {
      cart_data[id].count++;
    } else {
      item.count = 1;
      cart_data[id] = item;
    }
    localStorage.setItem("cart", JSON.stringify(cart_data));
    console.log(cart_data[id].count);
  }
});
